<?php
require_once "../../config.php";
require_once "../../functions.php";
$currentPage = $_POST['currentPage'];
$pageSize = $_POST['pageSize'];
//获取筛选条件
$status = $_POST['status'];
$categoryId = $_POST['categoryId'];
$offset = ($currentPage - 1) * $pageSize;
//获取文章的数据，返回给前端
//连接数据库
$connect = connect();
$where = " where 1=1 ";
if($status != 'all'){
    $where .= " and p.status = '{$status}'";
}

if($categoryId != 'all'){
    $where .= " and p.category_id = '{$categoryId}'";;
}

$sql = "select p.id,p.title,p.created,p.status,u.nickname,c.name from posts p left join users u on u.id = p.user_id left join categories c on c.id = p.category_id".$where." limit {$offset},{$pageSize}";
// echo $sql;
// die;
$result = query($connect,$sql);
//获取总页数
$sql1 = "select count(*) as num from posts p ".$where;
// echo $sql1;
$result1 = query($connect,$sql1);
// print_r($result1);
$num = $result1[0]['num'];
$response = ["code"=>0,"msg"=>"操作失败"];
if($result){
    $response["code"] = 1;
    $response["msg"] = "操作成功";
    $response["data"] = $result;
    $response['num'] = $num;
}
header('content-type:application/json;charset=utf8');
echo json_encode($response);
?>